﻿using AutoMapper;
using Modules;
using PmSoft;
using PmSoft.Entity;
using PmSoft.Logging;

namespace ManagementApi.Models.System.TableLog
{
    public class SysTableLogForm
    {
        /// <summary>
        /// 日志主键 
        ///</summary>
        public int Id { get; set; }
        /// <summary>
        /// 操作类型名称
        /// </summary>
        public string OperationTypeName { get; set; }
        /// <summary>
        /// 数据库名 
        ///</summary>
        public string DatabaseName { get; set; }
        /// <summary>
        /// 表名 
        ///</summary>
        public string TableName { get; set; }
        /// <summary>
        /// 对象ID 
        ///</summary>
        public string ObjectId { get; set; }
        /// <summary>
        /// 操作数据 
        ///</summary>
        public string Data { get; set; }
        /// <summary>
        /// 操作者安全信息ID 
        ///</summary>
        public int? SecurityId { get; set; }
        /// <summary>
        /// 操作用户ID 
        ///</summary>
        public int UserId { get; set; }
        /// <summary>
        /// 操作用户类型
        /// </summary>
        public string UserTypeName { get; set; }
        /// <summary>
        /// 操作者名称 
        ///</summary>
        public string Operator { get; set; }
        /// <summary>
        /// 操作IP 
        ///</summary>
        public string Ip { get; set; }
        /// <summary>
        /// 操作IP归属地
        /// </summary>
        public string IpGeo { get; set; }
        /// <summary>
        /// 操作URL 
        ///</summary>
        public string Url { get; set; }
        /// <summary>
        /// 创建时间 
        ///</summary>
        public DateTime CreateTime { get; set; }
    }

    public static partial class SysTableLogExceptions
    {
        public static SysTableLogForm AsForm(this SysTableLog entity)
        {
            SysTableLogForm form = new();
            var mapper = ServiceAccessor.GetService<IMapper>();
            mapper.Map(entity, form);
            form.OperationTypeName = entity.OperationType.GetDescription();
            form.UserTypeName = entity.UserType.GetDescription();
            return form;
        }
    }
}
